package com.xf.tourismapp.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xf.tourismapp.R;
import com.xf.tourismapp.activities.MainActivity;
import com.xf.tourismapp.entity.User;
import com.xf.tourismapp.util.HttpCallbackListener;
import com.xf.tourismapp.util.HttpUtil;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class UserEditorFragment extends Fragment {

    private String originAddress = "http://2qmaev.natappfree.cc/student_manager/user/updateUser";
    private User user;
    private EditText editPhone;
    private EditText editUsername;
    private EditText editPwd;
    private Button btnUpdate;
    private Button btnReset;
private Context mContext;
    public void setUser(User user) {
        this.user = user;
    }

    public static UserEditorFragment newInstance() {
        UserEditorFragment fragment = new UserEditorFragment();
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.mContext = getActivity();

        return inflater.inflate(R.layout.fragment_user_editor, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        editPhone = view.findViewById(R.id.editPhone);
        editUsername = view.findViewById(R.id.editUsername);
        editPwd = view.findViewById(R.id.editPwd);
        btnReset = view.findViewById(R.id.btn_reset);
        btnUpdate = view.findViewById(R.id.bet_update);
        if (user.getPhone() != null) {
            editPhone.setText(user.getPhone());
            editPhone.setEnabled(false);
        }
        if (user.getUsername() != null)
            editUsername.setText(user.getUsername());
        if (user.getPassword() != null)
            editPwd.setText(user.getPassword().toString());
        btnReset.setOnClickListener((View v) -> {
            editUsername.setText("");
            editPwd.setText("");
        });
        btnUpdate.setOnClickListener((view2 -> {
            HashMap<String, String> hashMap = new HashMap<>();
            hashMap.put("username",editUsername.getText().toString());
            hashMap.put("password",editPwd.getText().toString());
            hashMap.put("id",user.getId()+"");
            try {
                String url = HttpUtil.getURLWithParams(originAddress,hashMap);
                HttpUtil.sendHttpRequest(url, new HttpCallbackListener() {
                    @Override
                    public void onFinish(String response) {

                        JSONObject jsonObject = JSONObject.parseObject(response);
                        if (jsonObject.getString("code").equals("200")){

                            Looper.prepare();
                            Toast.makeText(getActivity(), "修改成功",Toast.LENGTH_LONG).show();
                            getFragmentManager().popBackStack();
                            HomePageFragment.getHomePageFragmentInstance().findUserListByPage(String.valueOf(HomePageFragment.getHomePageFragmentInstance().getPageVO().getPageIndex()), "10");
                            Looper.loop();
                        }else {
                            Toast.makeText(getActivity(),"修改失败",Toast.LENGTH_LONG);

                        }
                    }


                    @Override
                    public void onError(Exception e) {
                        Toast.makeText(getActivity(),"修改失败",Toast.LENGTH_LONG);
                    }
                });
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }));
        super.onViewCreated(view, savedInstanceState);
    }
}